$.fn.dataTableExt.oApi.fnExtStylePagingInfo = function(oSettings) {
	return {
		"iStart" : oSettings._iDisplayStart,
		"iEnd" : oSettings.fnDisplayEnd(),
		"iLength" : oSettings._iDisplayLength,
		"iTotal" : oSettings.fnRecordsTotal(),
		"iFilteredTotal" : oSettings.fnRecordsDisplay(),
		"iPage" : oSettings._iDisplayLength === -1 ? 0 : Math
				.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
		"iTotalPages" : oSettings._iDisplayLength === -1 ? 0 : Math
				.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
	};
};

$.fn.dataTableExt.oPagination.extStyle = {

	"fnInit" : function(oSettings, nPaging, fnCallbackDraw) {

		var oPaging = oSettings.oInstance.fnExtStylePagingInfo();

		nFirst = $(' <i class="fa fa-step-backward first" title="首页"></i>');
		nPrevious = $('<i class="fa fa-backward previous" title="上一页"></i> ');
		nNext = $('<i class="fa  fa-forward next" title="下一页"></i>');
		nLast = $('<i class="fa fa-step-forward last" title="末页"></i>');
		nPageOf = $('<span>共 <strong>' + oPaging.iTotalPages
				+ '</strong> 条记录，<strong>1/1</strong>&nbsp;&nbsp;</span>');

		$(nPaging).append(nPageOf).append(nFirst).append(nPrevious)
		// .append(nPageTxt)
		// .append(nPageNumBox)

		// .append(nTotalPages)
		.append(nNext).append(nLast)

		nFirst.click(function() {
			if ($(this).hasClass("disabled"))
				return;
			oSettings.oApi._fnPageChange(oSettings, "first");
			fnCallbackDraw(oSettings);
		}).bind('selectstart', function() {
			return false;
		});

		nPrevious.click(function() {
			if ($(this).hasClass("disabled"))
				return;
			oSettings.oApi._fnPageChange(oSettings, "previous");
			fnCallbackDraw(oSettings);
		}).bind('selectstart', function() {
			return false;
		});

		nNext.click(function() {
			if ($(this).hasClass("disabled"))
				return;
			oSettings.oApi._fnPageChange(oSettings, "next");
			fnCallbackDraw(oSettings);
		}).bind('selectstart', function() {
			return false;
		});

		nLast.click(function() {
			if ($(this).hasClass("disabled"))
				return;
			oSettings.oApi._fnPageChange(oSettings, "last");
			fnCallbackDraw(oSettings);
		}).bind('selectstart', function() {
			return false;
		});
		/*
		 * nPageNumBox.change(function () { var pageValue =
		 * parseInt($(this).val(), 10) - 1 ; // -1 because pages are 0 indexed,
		 * but the UI is 1 var oPaging = oSettings.oInstance.fnPagingInfo();
		 * 
		 * if(pageValue === NaN || pageValue<0 ){ pageValue = 0; }else
		 * if(pageValue >= oPaging.iTotalPages ){ pageValue =
		 * oPaging.iTotalPages -1; } oSettings.oApi._fnPageChange(oSettings,
		 * pageValue); fnCallbackDraw(oSettings); });
		 */

	},

	"fnUpdate" : function(oSettings, fnCallbackDraw) {
		if (!oSettings.aanFeatures.p) {
			return;
		}

		var oPaging = oSettings.oInstance.fnExtStylePagingInfo();

		/* Loop over each instance of the pager */
		var an = oSettings.aanFeatures.p;

		$(an).find('span.paginate_total').html(oPaging.iTotalPages);
		$(an).find('.pageinate_input_box').val(oPaging.iPage + 1);

		$(an).each(function(index, item) {

			var $item = $(item);

			if (oPaging.iPage == 0) {
				var prev = $item.find('i.first').add($item.find('i.previous'));
				prev.addClass("disabled");
			} else {
				var prev = $item.find('i.first').add($item.find('i.previous'));
				prev.removeClass("disabled");
			}

			if (oPaging.iPage + 1 == oPaging.iTotalPages) {
				var next = $item.find('i.last').add($item.find('i.next'));
				next.addClass("disabled");
			} else {
				var next = $item.find('i.last').add($item.find('i.next'));
				next.removeClass("disabled");
			}
		});
	}
};

var Grid = function() {
	var a = false;
	return {
		initGridDefault : function() {
			$.extend($.fn.dataTable.defaults, {

				dom : "<'row'<'col-sm-12'tr>>"
						+ "<'row paging_tool' <'col-sm-5' B><'col-sm-7' p>>",
				sPaginationType : 'extStyle',
				lengthMenu: [
				             [ 10, 25, 50, 100 ],
				             [ '10行', '25 行', '50 行', '100行' ]
				         ],
				oLanguage : {
					oAria : {
						sSortAscending : ": 升序排列",
						sSortDescending : ":降序排列"
					},
					oPaginate : {
						sFirst : "第一页",
						sLast : "最后一页",
						sNext : "下一页",
						sPrevious : "上一页"
					},
					sEmptyTable : "没有数据",
					sInfo : "显示 _START_ 到 _END_ ，共 _TOTAL_ 条",
					sInfoEmpty : "没有数据",
					sInfoFiltered : "(从 _MAX_ 条中过滤)",
					sInfoPostFix : "",
					sDecimal : "",
					sThousands : ",",
					sLengthMenu : "显示 _MENU_ 条",
					buttons:{pageLength : "显示 %d 条"},
					sLoadingRecords : "加载中...",
					sProcessing : "处理中...",
					sSearch : "从当前数据中检索:",
					sUrl : "",
					sZeroRecords : "没有找到匹配的记录"
				}
			});
			Grid.createButton();
			a = true;
		},// initGridDefault end
		createButton : function() {
			$.fn.dataTable.ext.buttons.checkAll = {
				text : '全选',
				className : 'btn btn-primary',
				action : function(e, dt, node, config) {
					//dt.ajax.reload();
				}
			};
			$.fn.dataTable.ext.buttons.checkInverse = {
				text : '反选',
				action : function(e, dt, node, config) {
					//dt.ajax.reload();
				}
			};
			$.fn.dataTable.ext.buttons.buutonOther = {
				extend : 'collection',
				text : '其它 <span class="caret">',
			
				
				buttons : [ {
					text : '<i class="fa fa-print"/>&nbsp;&nbsp;打印',
					action : function(e, dt, node, config) {
					}
				}, {
					text : '<i class="fa fa-file-excel-o"/>&nbsp;&nbsp;导出Excel',
					action : function(e, dt, node, config) {
					}
				}, {
					text : '<i class="fa fa-file-word-o"/>&nbsp;&nbsp;导出Word',
					action : function(e, dt, node, config) {
					}
				}, {
					text : '<i class="fa fa-file-pdf-o"/>&nbsp;&nbsp;导出PDF',
					action : function(e, dt, node, config) {
					}
				}, {
					text : '<i class="fa fa-cog"/>&nbsp;&nbsp;设置',
					action : function(e, dt, node, config) {
					}
				} ]
			};
		},
		initAjax : function(b) {
			if (b == undefined) {
				b = $("body")
			}
			if (!a) {
				Grid.initGridDefault()
			}

			$('table[data-grid="table"]', b).each(function() {

				Grid.initDataGrid($(this));
			})
		},//initGrid end

		initDataGrid : function(X) {
		
			var dt = $('#storyList').DataTable({buttons:['checkAll','checkInverse','pageLength','buutonOther']});
			
			
			//dt.buttons().container()
	        //.appendTo( $(X).find('col-sm-5:eq(0)') );
			
			//dt.button().add( 0, 'buutonOther');
			
		}
	}
}();